import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/component_information_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';

class PartsAccessoryListController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? search;

  /// 筛选条件列表
  List<FilterBean> filterList = [];

  /// 状态筛选条件
  int? status;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 状态筛选条件
    final List<FilterEntitiesBean> statusList = [];
    statusList.add(FilterEntitiesBean(name: '正常', value: 0));
    statusList.add(FilterEntitiesBean(name: '待检定', value: 1));
    statusList.add(FilterEntitiesBean(name: '待更换', value: 2));
    statusList.add(FilterEntitiesBean(name: '待检测', value: 3));
    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '状态', filterList: statusList);
    filterList.add(filterBean);
  }

  /// 列表接口
  Future<List<ComponentInformationEntity>> getListData(int pageNum, int pageSize) async {
    // 获取列表接口
    final res = await HttpUtil().generateGetRequest<List<ComponentInformationEntity>>(
      Interface.getInformationPage,
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'componentName': search,
        'status': status,
      },
    ).execute();
    List<ComponentInformationEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  } // 获取列表数据

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    status = null;
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '状态') {
        status = i.chosenValue[0].value as int;
      }
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
